<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <treeTools :tree-node="company" :is-root="true" @showAialog="addAialog" />
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <treeTools
            slot-scope="{ data }"
            :tree-node="data"
            @delDeparts="getDepartMents"
            @addAialog="addAialog"
            @editAialog="editAialog"
          />
        </el-tree>
      </el-card>

      <!-- Dalog 弹窗 -->
      <AddDepts ref="addDepts" :show-dialog.sync="dialogVisible" :node="node" @success="getDepartMents" />
    </div>
  </div>
</template>

<script>
import treeTools from '@/views/departments/component/tree-tools.vue'
import AddDepts from '@/views/departments/component/AddDepts'
import { getDepartmentList } from '@/api/departments'
import { tranListToTreeData } from '@/utils/validate'
export default {
  components: {
    treeTools,
    AddDepts
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: { name: '传智教育', manager: '负责人' },
      dialogVisible: false, // 控制对话框是否显示
      node: {}, // 当前操作的本门节点
      loading: false // 控制页面加载等待效果
    }
  },
  created() {
    this.getDepartMents()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    // 获取框架部门列表
    async getDepartMents() {
      this.loading = true
      const res = await getDepartmentList()
      this.company = { name: res.companyName, manager: '负责人' }
      this.departs = tranListToTreeData(res.depts, '')
      this.loading = false
    },
    // 点击新增对话框
    addAialog(node) {
      this.node = node
      this.dialogVisible = true
    },
    // 点击编辑对话框
    editAialog(node) {
      this.node = node
      this.dialogVisible = true
      this.$refs.addDepts.getDepartmentDetail(node.id)
    }
  }
}
</script>

<style lang="scss">
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
